package com.example.classcommunity3.controller;

import com.example.classcommunity3.service.HomeworkService;
import com.example.classcommunity3.service.SubmitHomeworkService;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@RestController
@RequestMapping(value = "/homework")
public class HomeworkController {

    @Autowired
    HomeworkService homeworkService;

    @Autowired
    SubmitHomeworkService submitHomeworkService;

    @RequestMapping(params = "method=findAllSubmit")
    public String findAllSubmit(Long hId) {
        /**
         @describe 找到某次作业下的所有已经提交了的信息
         @param [hId]
         @return java.lang.String -> json{stuName, stuAccount, sItem, sTime, sFileUrl}
         @date 2018/6/7
         */
        String json = submitHomeworkService.findAllSubmit(hId);
        System.out.println(json);
        return json;
    }

    @RequestMapping(params = "method=deleteHomeworkByHid")
    public boolean deleteHomework(Long hId) {
        /**
         @describe 教师删除作业
         @param [hId]
         @return boolean
         @date 2018/6/8
         */
        return homeworkService.deleteHomeworkByHid(hId);
    }

    @RequestMapping(params = "method=findHaveSubmit")
    public boolean findHaveSubmit(Long hId, Long account) {
        /**
         @describe 判断是否提交过作业
         @param [hId, account]
         @return boolean
         @date 2018/6/9
         */
        return submitHomeworkService.findHaveSubmit(hId, account);
    }

    @RequestMapping(params = "method=disSubmit")
    public boolean disSubmit(Long hId, Long account){

        boolean b;
        b=submitHomeworkService.disSubmit(hId,account);
        return b;
    }
    //homeworkId, file, item, stuAccount, time

    //上传作业
    @RequestMapping(value = "/submitHomework")
    public void submitHomework(Long homeworkId, Long stuAccount, String item, String submitTime,
                               MultipartFile file, HttpServletRequest request) {
        /**
         @describe 学生提交作业，上传文件
         @param [homeworkId, stuAccount, item, submitTime, file, request]
         @return void
         @date 2018/6/9
         */
        System.out.println("作业id" + homeworkId);
        System.out.println("学生账号" + stuAccount);
        System.out.println("作业内容" + item);
        System.out.println("提交日期" + submitTime);

        boolean b = false;
        MultipartFile file1;
        String fileName = "";
        String url = "D:\\submitHomework";
        try {
            file1 = file;
            //得到文件名
            fileName = file1.getOriginalFilename();
            //得到新的文件
            File file2 = new File(url, fileName);
            //得到原始文件输入流
            InputStream inputStream = file1.getInputStream();
            //得到新的文件输入流
            FileOutputStream fileOutputStream = new FileOutputStream(file2);
            IOUtils.copy(inputStream, fileOutputStream);
            inputStream.close();
            fileOutputStream.close();
            System.out.println("上传成功");
            b = true;

        } catch (Exception e) {
            e.printStackTrace();
            b = false;
        }

        if (b)
            submitHomeworkService.submitHomeworkWithFile(url, fileName, homeworkId, stuAccount, item, submitTime);
        else
            submitHomeworkService.submitHomeworkWithoutFile(homeworkId, stuAccount, item, submitTime);
    }


    @RequestMapping(value = "/downloadHomework")
    public String downloadHomework(String fileName, HttpServletRequest request,
                                   HttpServletResponse response) {
        /**
         @describe 文件下载，给出文件名，文件地址
         @param [fileName, url, request, response]
         @return java.lang.String
         @date 2018/6/8
         */
        String url = "D:\\submitHomework";
        System.out.println("下载文件地址:" + url);
        System.out.println("下载文件名：" + fileName);
        if (!fileName.equals("")) {
            url = url + "\\";
            File file = new File(url, fileName);
            if (file.exists()) {
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    System.out.println("success");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return null;
    }

    @RequestMapping(value = "/downloadAllHomework")
    public String downloadAllHomework(Long hId, HttpServletRequest request,
                                       HttpServletResponse response) {
        /**
         @describe 批量打包文件下载，给出文件名，文件地址
         @param [fileName, url, request, response]
         @return java.lang.String
         @date 2018/6/8
         */
        String url = "D:\\submitHomework\\zip";
        String fileName = submitHomeworkService.findZipFile(hId);

        System.out.println("下载文件地址:" + url);
        System.out.println("下载文件名：" + fileName);
        if (!fileName.equals("")) {
            url = url + "\\";
            File file = new File(url, fileName);
            if (file.exists()) {
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"; filename*=utf-8''" + fileName);
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    System.out.println("success");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return null;
    }


}


